package id.co.rho.scoring.cid.server.service.impl;

import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandResult;
import id.co.rho.scoring.cid.domain.Model;
import id.co.rho.scoring.cid.server.dao.GenericDao;
import id.co.rho.scoring.cid.server.dao.ModelSearchDao;
import id.co.rho.scoring.cid.server.service.ModelSearchService;
import id.co.rho.scoring.cid.shared.model.BaseDTOModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.extjs.gxt.ui.client.data.ModelData;

@Service
@Transactional
public class ModelSearchServiceImpl implements ModelSearchService {

	@PersistenceContext
	EntityManager entityManager;

	private GenericDao genericDao;
	private ModelSearchDao modelSearchDao;

	@Autowired
	public void setGenericDao(GenericDao genericDao) {
		this.genericDao = genericDao;
	}

	@Autowired
	public void setModelSearchDao(ModelSearchDao modelSearchDao) {
		this.modelSearchDao = modelSearchDao;
	}

	@Override
	public ModelSearchCommandResult searchCriteria(String modelName, String type,
			String revision) {
				
		ModelSearchCommandResult result = new ModelSearchCommandResult();
		List<Model> scoringModels = new ArrayList<Model>();
		List<ModelData> models = new ArrayList<ModelData>();

		String name = modelName == null?"":modelName;

		if (type != null && revision == null) {
			scoringModels = modelSearchDao.searchCriteria(name, type);
		} else if (type == null && revision != null) {
			scoringModels = modelSearchDao.searchCriteria(name,
					Integer.parseInt(revision));
		} else if (type != null && revision != null) {
			scoringModels = modelSearchDao.searchCriteria(name, type,
					Integer.parseInt(revision));
		} else {
			scoringModels = modelSearchDao.searchCriteria(name);
		}
				
		for (Model model :  scoringModels) {
			ModelData md = new BaseDTOModel();
			md.set("id", model.getId());
			md.set("name", model.getModelName());
			md.set("type", model.getTypeModelConfig());
			md.set("revision", model.getRevision());
			md.set("active", model.getAktifStatus());
			models.add(md);
		}
		result.setModels((Serializable) models);
		return result;		
	}

	@Override
	public ModelSearchCommandResult searchCriteria(String name, String type,
			String revision, Boolean active) {
		if (name == null){
			name = "";
		}
				
		ModelSearchCommandResult result = new ModelSearchCommandResult();
		List<Model> scoringModels = new ArrayList<Model>();
		List<ModelData> models = new ArrayList<ModelData>();

		if (type != null && revision == null) {
			scoringModels = modelSearchDao.searchCriteria(name, type,active);
		} else if (type == null && revision != null) {
			scoringModels = modelSearchDao.searchCriteria(name,
					Integer.parseInt(revision),active);
		} else if (type != null && revision != null) {
			scoringModels = modelSearchDao.searchCriteria(name, type,
					Integer.parseInt(revision),active);
		} else {
			scoringModels = modelSearchDao.searchCriteria(name,active);
		}
				
		for (Model model :  scoringModels) {
			ModelData md = new BaseDTOModel();
			md.set("id", model.getId());
			md.set("name", model.getModelName());
			md.set("type", model.getTypeModelConfig());
			md.set("revision", model.getRevision());
			md.set("active", model.getAktifStatus());
			models.add(md);
		}
		result.setModels((Serializable) models);
		return result;		
	}

}
